package com.example.blockchain.service.impl;


import com.example.blockchain.entity.TradeObject;
import com.example.blockchain.entity.block.Mining;
import com.example.blockchain.entity.block.Pending;
import com.example.blockchain.utils.SQLiteHelper;
import com.example.blockchain.utils.encrypt.EncryptUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PendingServiceImpl {

    public static void save(Pending pending) throws Exception {
        Connection connection = null;
        try {
            connection = SQLiteHelper.getConnection();
            connection.setAutoCommit(false);
            String sql = "insert into [pending]([orderNo],[tradeBody],[createTime],[tradeType]) values(?,?,?,?);";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, pending.getOrderNo()+ "");
            statement.setString(2, pending.getTradeBody());
            statement.setString(3, pending.getCreateTime());
            statement.setString(4, pending.getTradeType());
            statement.execute();
            connection.commit();
        } catch (Exception e) {
            if(connection != null)
                try {
                    connection.rollback();
                } catch (Exception e1) {
                    e.getMessage();
                }
            throw new Exception(e.getMessage());
        }finally {
            SQLiteHelper.close(connection);
        }

    }
    public static boolean deletePendings(List<String> tradeNos) {
        if(tradeNos == null || tradeNos.size() == 0) {
            return true;
        }
        Connection connection = null;
        try {
            connection = SQLiteHelper.getConnection();
            connection.setAutoCommit(false);
            PreparedStatement statement = connection.prepareStatement("delete from [pending] where [orderNo]=?");
            for(int i = 0; i< tradeNos.size(); i++) {
                System.out.println(tradeNos.get(i));
                statement.setString(1, tradeNos.get(i));
                statement.addBatch();
                if(i % 100 ==0) {
                    statement.executeBatch();
                    statement.clearBatch();
                }
            }
            statement.executeBatch();
            statement.clearBatch();
            connection.commit();
            return true;
        } catch (Exception e) {
            try {
                if(connection != null) {
                    connection.rollback();
                }
            } catch (SQLException e1) {
                e.getMessage();

            }
            e.printStackTrace();
        }finally {
            SQLiteHelper.close(connection);
        }
        return false;
    }

    /**
     * order by gas
     * order by createTime
     * @return
     */
    public static List<Pending> queryPendings() {
        List<Pending> pes = new ArrayList<Pending>();
        Connection connection = null;
        try {
            connection = SQLiteHelper.getConnection();
            String sql = "select * from [pending] order by createTime desc;;";
            Statement statement=connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Pending ps = new Pending();
                ps.setCreateTime(rs.getString("createTime"));
                ps.setOrderNo(rs.getString("orderNo"));
                ps.setTradeBody(rs.getString("tradeBody"));
                ps.setTradeType(rs.getString("tradeType"));
                pes.add(ps);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.getMessage();

        } finally {
            SQLiteHelper.close(connection);
        }
        return pes;
    }
    public static String genTradeNo(TradeObject tb) {
        String a = tb.toString();
        return genTradeNo(a);
    }
    public static String genTradeNo(String tbstring) {
        return Mining.TRADE_NUMBER_PREFIX + EncryptUtil.encryptSHA1(tbstring);
    }
    public static void validateTradeNo(TradeObject tb) throws Exception {
        String tradeNo = PendingServiceImpl.genTradeNo(tb);
        if(!tradeNo.equals(tb.getHashNo())) {
            throw new Exception();
        }
    }

}
